package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody Category category) {
        log.info("category:{}", category);
        categoryService.save(category);
        return R.success("添加分类成功");
    }

    /**
     * 分页查询分类
     *
     * @param
     * @return
     */
    @GetMapping("/page")
    public R selectCategoryByPage(Integer page, Integer pageSize) {
        log.info("page:{},pageSize:{}", page, pageSize);

        //分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);

        //条件构造器,添加排序条件
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Category::getSort);

        //调用service分页查询
        categoryService.page(pageInfo, qw);
        long total = pageInfo.getTotal();
        List<Category> records = pageInfo.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("records", records);
        return R.success(map);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteCategory(Long id) {
        //1、检查当前分类下是否有对应的菜品
// SELECT COUNT(*) FROM `dish` WHERE category_id = 1397844263642378242
// 结果大于0 ，则不能删除  代码结束 return R.error("当前分类下有菜品，不能删除")

//2、检查当前分类下是否有对应的套餐
// SELECT COUNT(*) FROM `setmeal` WHERE category_id= 1397844263642378242
//结果大于0 ，则不能删除  代码结束 return R.error("当前分类下有套餐，不能删除")

//3、可以删除,则调用mapper直接删除 categoryMapper.deleteById(id);
        log.info("删除分类,id为:{}",id);
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R updateCategory(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 查询菜品
     * @param
     * @return
     */
    @GetMapping("/list")
    public R selectDishByType(Category category){
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(category.getType()!=null,Category::getType,category.getType());
        qw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(qw);
        return R.success(list);
    }

}
